package com.woniuxy.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.ExamPaper;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.Test;
import com.woniuxy.service.ExamPaperService;
import com.woniuxy.service.TestExamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * (ExamPaper)表控制层
 *
 * @author zjx
 * @since 2021-05-14 19:42:02
 */
@RestController
@RequestMapping("/examPaper")
@Api(tags = "试卷接口")
@CrossOrigin
public class ExamPaperController {
    /**
     * 服务对象
     */
    @Resource
    private ExamPaperService examPaperService;
    @Resource
    private TestExamService testExamService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation("通过试卷id查询单条数据")
    @GetMapping(value = "/selectOne",produces = {"application/json;charset=utf-8"})
    public ResponseEntity selectOne(@ApiParam("试卷id") Integer id) {
        ResponseEntity resultMsg = new ResponseEntity(201,"查询失败",null);
        ExamPaper examPaper = this.examPaperService.queryById(id);
        if (examPaper!=null) {
            resultMsg.setCode(200);
            resultMsg.setData(examPaper);
            resultMsg.setMessage("查询成功!");
        }
        return resultMsg;
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @ApiOperation("查询多条数据")
    @GetMapping(value = "/selectAllByLimit",produces = {"application/json;charset=utf-8"})
    public ResponseEntity selectAllByLimit(@ApiParam("查询起始位置") int offset, @ApiParam("查询条数") int limit) {
        ResponseEntity resultMsg = new ResponseEntity(201,"查询失败",null);
        List<ExamPaper> examPaperList = this.examPaperService.queryAllByLimit(offset, limit);
        if (examPaperList!=null) {
            resultMsg.setCode(200);
            resultMsg.setData(examPaperList);
            resultMsg.setMessage("查询成功!");
        }
        return resultMsg;
    }
    /**
     * 新增数据
     *
     * @param examPaper 实例对象
     * @return 实例对象
     */
    @ApiOperation("新增数据:传入试卷对象")
    @PostMapping(value = "/insert",produces = {"application/json;charset=utf-8"})
    public ResponseEntity insert(@ApiParam("试卷对象") ExamPaper examPaper) {
        ResponseEntity resultMsg = new ResponseEntity(201,"新增失败",null);
        int row = this.examPaperService.insert(examPaper);
        if (row!=0) {
            resultMsg.setCode(200);
            resultMsg.setMessage("新增成功!");
        }
        return resultMsg;
    }

    /**
     * 修改数据
     *
     * @param examPaper 实例对象
     * @return 实例对象
     */
    @ApiOperation("修改数据")
    @PutMapping(value = "/{id}", produces = {"application/json;charset=utf-8"})
    public ResponseEntity update(@ApiParam("试卷对象")ExamPaper examPaper) {
        ResponseEntity resultMsg = new ResponseEntity(201, "修改失败", null);
        int row = this.examPaperService.update(examPaper);
        if (row != 0) {
            resultMsg.setCode(200);
            resultMsg.setMessage("修改成功!");
        }
        return resultMsg;
    }

    /**
     * 通过主键删除数据
     *
     * @param ids 主键数组
     * @return 是否成功
     */
    @ApiOperation("通过主键删除数据")
    @DeleteMapping(value = "/{deleteids}",produces = {"application/json;charset=utf-8"})
    public ResponseEntity deleteById(@ApiParam("试卷id数组")@PathVariable("deleteids") int[] ids) {
        ResponseEntity resultMsg = new ResponseEntity(200, "删除成功!", null);
        for (int id :ids) {
            int row =  this.examPaperService.deleteById(id);
            if (row == 0) {
                resultMsg.setCode(201);
                resultMsg.setMessage("删除失败!");
                return resultMsg;
            }
        }
        return resultMsg;
    }
    /**
     * 通过id删除试卷信息
     *
     * @param id 试卷id
     * @return  布尔值,是否成功
     */
    @ApiOperation("删除试卷信息:传入试卷id")
    @GetMapping("deleteExamPaper/{id}")
    public ResponseEntity deleteExamPaper(@ApiParam("试卷id")@PathVariable int id){
        int row = this.examPaperService.deleteById(id);
        String result = "删除试卷信息失败";
        if(row>0){
            result = "删除试卷信息成功";
        }
        return new ResponseEntity(200, result, null);
    }

    @ApiOperation("展示所有试卷信息")
    @PostMapping("showExamPaper")
    public ResponseEntity showExamPaper(@RequestBody Map<String,Object> map){
        int startPage = (int) map.get("page");
        int pageSize = (int) map.get("limit");
        JSONObject jsonObject=JSONObject.fromObject(map.get("examPaper"));
        ExamPaper examPaper = (ExamPaper)JSONObject.toBean(jsonObject, ExamPaper.class);
        PageInfo<Map<String,String>> pageInfo = this.examPaperService.showExamPaper(startPage,pageSize,examPaper);
        //此处有坑,若删掉的信息为当前页最后一条,此时前端将查不到数据
        if(pageInfo.getList().size()==0&&startPage>1){
            pageInfo = this.examPaperService.showExamPaper(--startPage,pageSize,examPaper);
        }
        return new ResponseEntity(200,"分页查询试卷信息成功",pageInfo);
    }

    /**
     * 添加试卷基础信息, 如果为随机组卷, 添加试题
     * @param map   试卷基础信息
     * @return 返回信息实体类
     */
    @ApiOperation("添加试卷基础信息")
    @PostMapping("addBasicExamPaper")
    public ResponseEntity addBasicExamPaper(@RequestBody Map<String,Object> map){
        String result = this.examPaperService.addBasicExamPaper(map);
        //result += this.testExamService.addRandomTest(testBankName,testLabel,examPaper);
        return new ResponseEntity(200,result,null);
    }
    @ApiOperation("随机组卷查询")
    @PostMapping("queryIdByTypeBankLabelLimit")
    public ResponseEntity queryIdByTypeBankLabelLimit(@RequestBody Map<String,Object> map){
        Map<String,Object> result = this.examPaperService.queryIdByTypeBankLabelLimit(map);
        return new ResponseEntity(200,"随机组卷查询成功",result);
    }
    @ApiOperation("以试题id集合查试题题干和标签")
    @PostMapping("queryExamPaperTestContent")
    public ResponseEntity queryExamPaperTestContent(@RequestBody Map<String,Object> map){
        List<Test> result = this.examPaperService.queryExamPaperTestContent(map);
        return new ResponseEntity(200,"以试题id集合查试题题干和标签成功",result);
    }
    @ApiOperation("穿梭框查询可选试题集合")
    @PostMapping("queryTestsByTypeLabelBodyBank")
    public ResponseEntity queryTestsByTypeLabelBodyBank(@RequestBody Map<String,Object> map){
        Map<String,Object> result = this.examPaperService.queryTestsByTypeLabelBodyBank(map);
        return new ResponseEntity(200,"穿梭框查询可选试题集合成功",result);
    }
    @ApiOperation("查看试卷")
    @GetMapping("viewTestPaper/{id}")
    public ResponseEntity viewTestPaper(@ApiParam("试卷id")@PathVariable("id") int id){
        ExamPaper result = this.examPaperService.viewTestPaper(id);
        return new ResponseEntity(200,"查看试卷成功",result);
    }
    @ApiOperation("查询所有试卷创建人")
    @GetMapping(value = "selectCreaters")
    public ResponseEntity selectCreaters(){
        List<String> creaters = this.examPaperService.selectCreaters();
        if(creaters.size()>0){
            return new ResponseEntity(200, "查询所有试卷创建人成功", creaters);
        }else{
            return new ResponseEntity(404, "查询所有试卷创建人失败", null);
        }
    }

}
